using System;
using System.Collections.ObjectModel;
using System.Drawing;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms.Integration;
using System.Windows.Input;

using Engine;
using Engine.Result;

using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.CommandWpf;

using InterfaceService.Testing;

using Mapping;

using ServiceImp;

namespace UIKitTutorials.ViewModel
{
    public class HomePageViewModel : ObservableObject
    {

        private bool _isTestChecked;
        private bool _isTestEnabled = true;
        public Lazy<MapView> Display;
        public MapView LoadMapView { get; }
        private bool IsInitialization;

        private ITestingService _TestingService;
        public HomePageViewModel()
        {
            _TestingService = new TestingService();
            Display = new Lazy<MapView>();
            LoadMapView = new MapView();
            _TestingService.OnTestDataReceived += OnTestDataReceived;
        }

        private void OnTestDataReceived(object sender, ChipResult e)
        {
            Display.Value.AddPoint((e.Id.X, e.Id.Y));


        }


        public bool IsTestChecked
        {
            get => _isTestChecked;
            set
            {
                if (value)
                {
                    _TestingService.Start();


                }
                else
                {
                    _TestingService.Stop();
                }

                Set(ref _isTestChecked, value);
            }
        }

        public bool IsTestEnabled
        {
            get => _isTestEnabled;
            set => Set(ref _isTestEnabled, value);
        }

        public AsyncCommand LotEndCommand => new AsyncCommand((obj) =>
        {
            _TestingService.Stop();
        });

        public AsyncCommand LotStartCommand => new AsyncCommand((obj) =>
                {
                    //DisplayControl.Child.Refresh();
                    //Display.Value.BackColor= Color.DeepPink;
                    //DisplayControl.Child.Refresh();
                    //Display.Value.Invalidate();
                    LoadMapView.Model2();

                });

        public AsyncCommand LoadWafer => new AsyncCommand((obj) =>
        {
            LoadMapView.Model2();


        });

        public AsyncCommand EmitChipCommand => new AsyncCommand(async (obj) =>
        {
            var tsk = Task.Run(() =>
              {

                  _TestingService.EmitChip();
              });

            await tsk;

        });
    }
}
